package gang.com.twoshopping.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;

import java.util.ArrayList;
import java.util.List;

import gang.com.twoshopping.R;
import gang.com.twoshopping.adapter.BaseAdapter;
import gang.com.twoshopping.adapter.HOTAdapter;
import gang.com.twoshopping.bean.Page;
import gang.com.twoshopping.bean.Wares;
import gang.com.twoshopping.html.Hunhe;
import gang.com.twoshopping.html.WareDetalActivity;
import gang.com.twoshopping.http.OkHttpHelper;
import gang.com.twoshopping.http.SpotsCallBack;
import gang.com.twoshopping.utils.Contants;
import gang.com.twoshopping.weight.DividerItemDecoration;
import okhttp3.Response;

/**
 * Created by xiaogangzai on 2017/5/7.
 */

public class HotFragment extends Fragment {


    private RecyclerView mRecyclerView;
    private List<Wares> mdata = new ArrayList<>();
    private HOTAdapter hotAdapter;
    private MaterialRefreshLayout swipe_refresh_layout_hot;
    private OkHttpHelper okhttphelper = OkHttpHelper.getInstance();
    private int currPage = 1;
    private int pageSize = 10;
    //总页数，用于判断是否需要加载更多
    private int totalPage = 1;
    public static final int NORMAL = 0;
    public static final int REFRESH = 1;
    public static final int LOADMORE = 2;
    private int state = NORMAL;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hot, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_hot);
        swipe_refresh_layout_hot = (MaterialRefreshLayout) view.findViewById(R.id.Material_hot);
        //子类继承父类，拥有父类所有的属性和方法，为了再省一部分代码，在写一个simpleAdapter抽象类继承BaseAdapter，
        //用匿名内部类的方法还会有问题，不能设置他的点击事件
       /* mRecyclerView.setAdapter(new BaseSimpleAdapter<HomeCategory>(getContext(), mdata, R.layout.template_home_cardview_l) {
            @Override
            public void bindData(BaseViewHolder viewHolder, HomeCategory homeCategory) {
             *//* TextView tt = (TextView) viewHolder.getView(R.id.text_title);
                tt.setText(homeCategory.getName());
                //单个子view的点击事件
                tt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });*//*
                //封装之后好处的比较
                viewHolder.getTextView(R.id.text_title).setText(homeCategory.getName());
            }
        });*/
        initRefreshLayout();
        getData();
        //三次封装
        return view;
    }

    //刷新数据
    private void refreshData() {
        currPage = 1;
        state = REFRESH;
        getData();
    }

    //加载更多数据
    private void loadMoreData() {
        currPage = ++currPage;
        state = LOADMORE;
        getData();
    }

    private void getData() {

        String url = Contants.ApI.WARES_HOT + "?curPage=" + currPage + "&pageSize=" + pageSize;
        okhttphelper.get(url, new SpotsCallBack<Page<Wares>>(getActivity()) {

            @Override
            public void onSucess(Response response, Page<Wares> waresPage) {
                mdata = waresPage.getList();
                currPage = waresPage.getCurrentPage();
                totalPage = waresPage.getTotalPage();
                showRecyclerView(mdata);
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });
    }

    private void initRefreshLayout() {
        swipe_refresh_layout_hot.setLoadMore(true);
        swipe_refresh_layout_hot.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                refreshData();
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                //如果当前页数小于总页数才可以加载的，大于的时候不可以加载，后台获取总页数
                if (currPage <= totalPage) {
                    loadMoreData();
                } else {
                    Toast.makeText(getActivity(), "已经到达底部了", Toast.LENGTH_LONG).show();
                    swipe_refresh_layout_hot.finishRefreshLoadMore();
                }
            }
        });
    }

    private void showRecyclerView(final List<Wares> mdatao) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                switch (state) {
                    case NORMAL:
                        hotAdapter = new HOTAdapter(getActivity(), mdatao);
                        mRecyclerView.setAdapter(hotAdapter);
                        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
                        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));

                        break;
                    case REFRESH:
                        hotAdapter.clearData();
                        hotAdapter.addData(mdatao);
                        //滑动到第一个位置
                        mRecyclerView.scrollToPosition(0);
                        swipe_refresh_layout_hot.finishRefresh();
                        break;
                    case LOADMORE:
                        //加载更多需要从当前最后一条数据往里面插
                        hotAdapter.addData(hotAdapter.getDates().size(), mdatao);
                        mRecyclerView.scrollToPosition(hotAdapter.getDates().size());
                        swipe_refresh_layout_hot.finishRefreshLoadMore();
                        break;
                }
                hotAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                    @Override
                    public void onClick(View v, int position) {
                        //类声明序列化什么时候用，在这里使用，直接传递一个对象,传递点击的那个对象,必须实现序列化的接口
                        Wares ware=hotAdapter.getItem(position);
                        Intent a = new Intent(getActivity(), WareDetalActivity.class);
                        a.putExtra(Contants.WARE,ware);
                        startActivity(a);
                    }
                });

            }
        });

    }
}
